		TITLE	CVMOD4 - Copyright (c) SLR Systems 1994

		INCLUDE	MACROS
		INCLUDE	MODULES
		INCLUDE	SEGMENTS
		INCLUDE	IO_STRUC
if	fg_pe
		INCLUDE	PE_STRUC
endif

		PUBLIC	CV_MODULE_4


		.DATA

		EXTERNDEF	CV_TEMP_RECORD:BYTE

		EXTERNDEF	CURNMOD_GINDEX:DWORD,BYTES_SO_FAR:DWORD,PE_BASE:DWORD

		EXTERNDEF	MODULE_GARRAY:STD_PTR_S,_FILE_LIST_GARRAY:STD_PTR_S,SEGMOD_GARRAY:STD_PTR_S,SEGMENT_GARRAY:STD_PTR_S
		EXTERNDEF	CSEG_GARRAY:STD_PTR_S,MDB_GARRAY:STD_PTR_S,PE_OBJECT_GARRAY:STD_PTR_S

		EXTERNDEF	CV_DWORD_ALIGN:DWORD


		.CODE	PASS2_TEXT

		EXTERNDEF	FLUSH_CV_TEMP:PROC,HANDLE_CV_INDEX:PROC,MOVE_TEXT_TO_OMF:PROC,MOVE_DSSIAX_TO_NEWOMF:PROC


MOD4_VARS	STRUC

LAST_VALID_SEGMOD_BP	DD	?
BASE_SM_START_BP	DD	?

MOD4_VARS	ENDS


FIX	MACRO	X

X	EQU	([EBP-SIZE MOD4_VARS].(X&_BP))

	ENDM


FIX	LAST_VALID_SEGMOD
FIX	BASE_SM_START


CV_MODULE_4	PROC
		;
		;OUTPUT MODULE RECORD FOR CODEVIEW 4.0
		;
		;FORMAT IS:
		;
		;	DW	OVERLAY #
		;	DW	LIB #
		;	DW	# OF CSEGMODS
		;	DW	'CV'
		;	SEGINFO	X DUP()
		;	NAME
		;
		PUSHM	EBP,EDI,ESI,EBX

		MOV	EBP,ESP
		ASSUME	EBP:PTR MOD4_VARS
		SUB	ESP,SIZE MOD4_VARS

		MOV	EDI,OFF CV_TEMP_RECORD
		MOV	ESI,CURNMOD_GINDEX

		CALL	CV_DWORD_ALIGN

		CONVERT	ESI,ESI,MODULE_GARRAY
		ASSUME	ESI:PTR MODULE_STRUCT

		MOV	EDX,[ESI]._M_LIBNUM
		MOV	EAX,BYTES_SO_FAR		;IN CASE THIS DOESN'T FIT IN ONE RECORD

		SHL	EDX,16
		PUSH	EAX

		MOV	[EDI],EDX		;LIB # AND OVERLAY #
		MOV	EDX,'VC' * 64K

		MOV	ESI,[ESI]._M_MDB_GINDEX
		XOR	ECX,ECX

		TEST	ESI,ESI
		JZ	L1$

		CONVERT	ESI,ESI,MDB_GARRAY
		ASSUME	ESI:PTR MDB_STRUCT

		MOV	ECX,[ESI]._MD_CSEG_COUNT
		MOV	ESI,[ESI]._MD_FIRST_CSEGMOD_GINDEX

		OR	EDX,ECX
L1$:
		MOV	[EDI+4],EDX		;# OF CSEGMODS AND 'CV'
		ADD	EDI,8

		TEST	ECX,ECX
		JZ	L5$
L2$:
		MOV	LAST_VALID_SEGMOD,ESI
		CONVERT	ESI,ESI,SEGMOD_GARRAY
		ASSUME	ESI:PTR SEGMOD_STRUCT
		MOV	ECX,[ESI]._SM_START

		MOV	BASE_SM_START,ECX
L31$:
		MOV	EAX,[ESI]._SM_LEN		;CALCULATE SEGMENT LENGTH
		MOV	ECX,BASE_SM_START

		MOV	EBX,[ESI]._SM_MODULE_CSEG_GINDEX
		SUB	EAX,ECX

		MOV	EDX,[ESI]._SM_BASE_SEG_GINDEX
		MOV	[EDI+8],EAX			;UPDATE LENGTH
		;
		;SEE IF NEXT SEGMOD MATCHES - MUST MATCH SEGMENT AND CSEG-ORDER
		;
		MOV	EAX,[ESI]._SM_NEXT_SEGMOD_GINDEX
		CONVERT	EBX,EBX,CSEG_GARRAY
		ASSUME	EBX:PTR CSEG_STRUCT
		MOV	ECX,[EBX]._CSEG_NEXT_CSEGMOD_GINDEX

		CMP	ECX,EAX
		JNZ	L35$

		TEST	EAX,EAX
		JZ	L35$

		CONVERT	EAX,EAX,SEGMOD_GARRAY
		ASSUME	EAX:PTR SEGMOD_STRUCT

		CMP	[EAX]._SM_BASE_SEG_GINDEX,EDX
		JNZ	L33$
L32$:
		MOV	ESI,EAX
		MOV	EAX,[EBX]._CSEG_NEXT_CSEGMOD_GINDEX

		MOV	LAST_VALID_SEGMOD,EAX
		JMP	L31$

L33$:
if	fg_pe
		GETT	CL,OUTPUT_PE
		MOV	ESI,[EAX]._SM_BASE_SEG_GINDEX

		OR	CL,CL
		JZ	L34$
		;
		;SEE IF PE_SECTION MATCHES
		;
		CONVERT	ESI,ESI,SEGMENT_GARRAY
		ASSUME	ESI:PTR SEGMENT_STRUCT
		CONVERT	EDX,EDX,SEGMENT_GARRAY
		ASSUME	EDX:PTR SEGMENT_STRUCT

		MOV	EDX,[EDX]._SEG_PEOBJECT_NUMBER
		MOV	ECX,[ESI]._SEG_PEOBJECT_NUMBER

		CMP	ECX,EDX
		JZ	L32$
endif
L34$:
		MOV	ESI,LAST_VALID_SEGMOD
		CONVERT	ESI,ESI,SEGMOD_GARRAY
		ASSUME	ESI:PTR SEGMOD_STRUCT

L35$:
		MOV	EDX,[ESI]._SM_MODULE_CSEG_GINDEX
		MOV	ESI,[ESI]._SM_BASE_SEG_GINDEX

		CONVERT	ESI,ESI,SEGMENT_GARRAY
		ASSUME	ESI:PTR SEGMENT_STRUCT

		MOV	ECX,BASE_SM_START
if	fg_pe
		GETT	AL,OUTPUT_PE
		MOV	EBX,[ESI]._SEG_CV_NUMBER

		OR	AL,AL
		JZ	L37$

		MOV	EBX,[ESI]._SEG_PEOBJECT_NUMBER
		MOV	ESI,[ESI]._SEG_PEOBJECT_GINDEX

		CONVERT	ESI,ESI,PE_OBJECT_GARRAY
		ASSUME	ESI:PTR PE_OBJECT_STRUCT

		MOV	EAX,PE_BASE

		SUB	ECX,EAX
		MOV	EAX,[ESI]._PEOBJECT_RVA

		SUB	ECX,EAX
		JMP	L38$

L37$:
endif
		ASSUME	ESI:PTR SEGMENT_STRUCT

		SUB	ECX,[ESI]._SEG_OFFSET
L38$:
		MOV	[EDI],EBX		;SEGMENT & PAD
		MOV	[EDI+4],ECX		;SEGMENT BASE

		ADD	EDI,12

		CMP	EDI,OFF CV_TEMP_RECORD+CV_TEMP_SIZE-12
		JC	L4$

		PUSH	EDX
		CALL	FLUSH_CV_TEMP
		POP	EDX
L4$:
		CONVERT	EDX,EDX,CSEG_GARRAY
		ASSUME	EDX:PTR CSEG_STRUCT

		MOV	ESI,[EDX]._CSEG_NEXT_CSEGMOD_GINDEX

		TEST	ESI,ESI
		JNZ	L2$

L5$:
		MOV	ESI,CURNMOD_GINDEX
		CONVERT	ESI,ESI,MODULE_GARRAY
		ASSUME	ESI:PTR MODULE_STRUCT

		;
		;NOW MOVE MODULE THEADR NAME...
		;
		CMP	EDI,OFF CV_TEMP_RECORD+CV_TEMP_SIZE-SYMBOL_TEXT_SIZE-2
		JC	L51$

		CALL	FLUSH_CV_TEMP
L51$:
		;
		;LIBRARIES DIFFERENT...
		;
		MOV	ECX,[ESI]._M_LIBNUM
		MOV	EAX,[ESI]._M_FILE_LIST_GINDEX

		TEST	ECX,ECX
		JZ	L55$
L52$:
		LEA	ESI,[ESI]._M_TEXT
		CALL	MOVE_TEXT_TO_OMF

		JMP	L57$

L55$:
		TEST	EAX,EAX
		JZ	L52$

		CONVERT	EAX,EAX,_FILE_LIST_GARRAY
		ASSUME	EAX:PTR FILE_LIST_STRUCT

		LEA	ESI,[EAX].FILE_LIST_NFN.NFN_TEXT
		MOV	EAX,[EAX].FILE_LIST_NFN.NFN_TOTAL_LENGTH

		CALL	MOVE_DSSIAX_TO_NEWOMF
L57$:
		CALL	FLUSH_CV_TEMP

		POP	EAX			;OLD BYTES_SO_FAR
		MOV	ECX,120H

		CALL	HANDLE_CV_INDEX

		CALL	CV_DWORD_ALIGN

		MOV	ESP,EBP

		POPM	EBX,ESI,EDI,EBP

		RET

CV_MODULE_4	ENDP


		END

